#include <string>
#include "Omvandla.h"

int Menu();
void AddStrings();
int GetNumber( int nrBits );
bool WithinLimits( int nrBits, int number );

int main( void )
{
	int choice;
	while( choice = Menu() != 0 )
	{
		switch( choice )
		{
		case 1:
			AddStrings();
		}
	}

	cin.get();
	return 0;
}

int Menu()
{
	int choice = 0;
	cout << string( 100, '\n');
	cout << "The DigSim Main Menu:" << endl;
	cout << "0. Exit" << endl;
	cout << "1. New calculation" << endl;

	cin >> choice;
	if( choice > 1 || choice < 0 )
		cout << "Invalid input, try again." << endl;

	return choice;
}

void AddStrings()
{
	int tal1, tal2, nrBits;
	Bitstr *str1 = new Bitstr();
	Bitstr *str2 = new Bitstr();
	Bitstr *sum = new Bitstr();

	cout << "How many bits do you want to use for each word?" << endl;
	cin >> nrBits;
	if( nrBits < 4 )
	{
		cout << "Can't have less than four bits, sry." << endl;
		cout << "Press ENTER to continue" << endl;
		cin.get();
		cin.get();
		return;
	}

	tal1 = GetNumber( nrBits );
	if( tal1 == 0 )
		return;

	tal2 = GetNumber( nrBits );
	if( tal2 == 0 )
		return;

	Omvandla::tal_str( tal1, str1, nrBits );
	Omvandla::tal_str( tal2, str2, nrBits );

	cout << "tal 1 decimal: " << tal1 << endl;
	cout << "str 1 binary: ";
	str1->print();

	cout << "tal 2 decimal: " << tal2 << endl;
	cout << "str 2 binary: ";
	str2->print();

	cout << "Adding str1 and str2" << endl;
	str1->addstr(*str2, sum );

	cout << "Sum binary is: ";
	sum->print();

	int sumDec = Omvandla::str_tal( *sum );
	cout << "Sum decimal is: " << sumDec << endl;

	cin.get();
	cin.get();

}

int GetNumber( int nrBits )
{
	int number;
	cout << "Enter your number: ";
	cin >> number;
	if( !WithinLimits( nrBits, number ) )
	{
		cout << "Number not within possible limit, try again" << endl;
		cout << "Press ENTER to continue" << endl;
		cin.get();
		cin.get();
		return 0;
	}
	return number;
}

bool WithinLimits( int nrBits, int number )
{
	if( number > -int(( pow(2,nrBits) )) )
		if( number < int( (pow(2,nrBits))-1 ) )
			return true;
	return false;
}